<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css" />
    <style>
        img {
            height: 30px;
        }
    </style>
</head>

<body>

    <body class="pear-container">
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="jqrlb-table" lay-filter="jqrlb-table"></table>
            </div>
        </div>
    </body>

    <script type="text/html" id="jqrlb-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		       创建
		    </button>
		</script>

    <script type="text/html" id="jqrlb-bar">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		<button class="pear-btn pear-btn pear-btn-sm" lay-event="remove">禁用</button>
	</script>
    <script type="text/html" id="avatar-tpl">
            <img src="{{d.avatar}}"/>
    </script>
    <script type="text/html" id="createTime-tpl">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}
</script>
<script type="text/html" id="id">
    {{d.LAY_TABLE_INDEX+1}}
</script>
    <script src="../../component/layui/layui.js"></script>
    <script src="../../component/pear/pear.js"></script>
    <script>
        layui.use(['table', 'form', 'jquery'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;


            let cols = [
                [
                {
                    title: '序号',
                    field: 'id',
                    align: 'center',
                    templet:"#id"
                },
                {
                    title: '机器人ID',
                    field: 'uid',
                    align: 'center'
                },
                {
                    title: '名称',
                    field: 'nickname',
                    align: 'center'
                },
                {
                    title: '头像',
                    field: 'avatar',
                    align: 'center',
                    templet: "#avatar-tpl"
                },
                {
                    title: '额外信息',
                    field: 'robotExtra',
                    align: 'center'
                },
                {
                    title: '时间',
                    field: 'createTime',
                    align: 'center',
                    templet: "#createTime-tpl"
                },
                {
                    title: '操作',
                    toolbar: '#jqrlb-bar',
                    align: 'center',
                    width: 130
                }
                ]
            ]

            table.render({
                elem: '#jqrlb-table',
                url: '/api/robot/list',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#jqrlb-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('tool(jqrlb-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            });

            table.on('toolbar(jqrlb-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                }
            });

            form.on('submit(jqrlb-query)', function (data) {
                table.reload('jqrlb-table', {
                    where: data.field
                })
                return false;
            });

            form.on('switch(jqrlb-enable)', function (obj) {
                layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
            });

            window.add = function () {
                layer.open({
                    type: 2,
                    title: '创建机器人',
                    shade: 0.1,
                    area: ['550px', '450px'],
                    content: 'jqr_add.html'
                });
            }

            window.edit = function (obj) {

                var data = obj.data;
                layer.open({
                    type: 2,
                    title: '机器人修改',
                    shade: 0.1,
                    area: ['500px', '500px'],
                    content: 'jqr_edit.html',
                    success: function (layero, index) {
                        var body = layui.layer.getChildFrame('body', index);
                        body.find("input[name='uid']").val(data.uid);
                        body.find("input[name='name']").val(data.nickname);
                        body.find("input[name='nickname']").val(data.nickname);
                        body.find("input[name='avatar']").val(data.avatar);
                        body.find("input[name='robotExtra']").val(data.robotExtra);
                        body.find("input[name='callback']").val(data.callback);
                        body.find("input[name='secret']").val(data.secret);

                        form.render();
                    }
                });


            }

            window.remove = function (obj) {
                layer.confirm('确定要禁用该机器人吗？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);

                    let loading = layer.load();
                    $.ajax({
                        url: '/api/robot/remove/' + obj.data.id,
                        dataType: 'json',
                        success: function (result) {
                            layer.close(loading);
                            if (result.code == 0) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 500
                                }, function () {
                                    table.reload('jqrlb-table');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 500
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function (param) {
                table.reload('jqrlb-table');
            }
        })
    </script>
</body>

</html>